package com.bbu.javaexam.controller;

import com.bbu.javaexam.domain.*;
import com.bbu.javaexam.service.FillQuestionService;
import com.bbu.javaexam.service.JudgeQuestionService;
import com.bbu.javaexam.service.MultiQuestionService;
import com.bbu.javaexam.service.PaperService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName PaperController.java
 * @Description TODO
 */
@RestController
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private JudgeQuestionService judgeQuestionService;
    @Autowired
    private MultiQuestionService multiQuestionService;
    @Autowired
    private FillQuestionService fillQuestionService;

    @GetMapping("/papers")
    public RespData<PaperManage> findAll() {
        RespData res = RespDataHandler.buildRespData(200, "请求成功", paperService.findAll());
        return res;
    }

    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId) {
        List<MultiQuestion> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);   //选择题题库
        List<FillQuestion> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);     //填空题题库
        List<JudgeQuestion> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);   //判断题题库
        Map<Integer, List<?>> map = new HashMap<>();
        map.put(1, multiQuestionRes);
        map.put(2, fillQuestionsRes);
        map.put(3, judgeQuestionRes);
        return map;
    }

    @PostMapping("/paperManage")
    public RespData add(@RequestBody PaperManage paperManage) {
        int res = paperService.add(paperManage);
        if (res != 0) {
            return RespDataHandler.buildRespData(200, "添加成功", res);
        }
        return RespDataHandler.buildRespData(400, "添加失败", res);
    }
}
